package com.pata.display
{
	import com.pata.enum.InvalidationType;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;

	public class Slider extends AbstractDisplay implements IDisplay
	{
		private static const HORIZONTAL:String  = "horizontal";
		private static const VERTICAL:String = "vertical";
		private static const BACKABLED:String = "backabled";
		private var sliderSkin:Object = {};
		private var mBackSkin:DisplayObject;
		private var mHandleSkin:BaseButton;
		public function set backSkin(value:DisplayObject):void
		{
			sliderSkin.back = mBackSkin;
		}
		public function set handleSkin(value:DisplayObject):void
		{
			sliderSkin.handle = mHandleSkin;
		}
		
		private var mOrientation:String = VERTICAL;
		public function set orientation(value:String):void
		{
			if(mOrientation != value)
			{
				directionChanged = true;
				mOrientation = value;
				this.invalidate( InvalidationType.STYLES);
			}
		}
		public function get orientation():String
		{
			return mOrientation;
		}
		
		private var mBackabled:Boolean = true;
		public function set backable(value:Boolean):void
		{
			if(mBackabled != value)
			{
				mBackabled = value;	
				this.invalidate( BACKABLED);
			}
		}
		public function get backable():Boolean
		{
			return mBackabled;
		}
		private var directionChanged:Boolean = false;
		public function Slider() 
		{
			super( this );
		}
		override protected function configUI():void
		{
			addChild( mBackSkin );
			
			addChild( mHandleSkin );
			mHandleSkin.addEventListener(MouseEvent.MOUSE_DOWN, onDrag);
			mHandleSkin.buttonMode = true;
			mHandleSkin.mouseChildren = false;
		}
		private function onDrag(e:MouseEvent):void
		{
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onSlider);
			stage.addEventListener(MouseEvent.MOUSE_UP, onDrop);
			if(mOrientation == HORIZONTAL)
			{
				mHandleSkin.startDrag(false, new Rectangle(0, 0, mWidth - mHeight, 0));
			}
			else
			{
				mHandleSkin.startDrag(false, new Rectangle(0, 0, 0, mHeight - mWidth));
			}
		}
		private function onDrop(e:MouseEvent):void
		{
			stage.removeEventListener(MouseEvent.MOUSE_UP, onDrop);
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, onSlider);
			mHandleSkin.stopDrag();
		}
		private function onSlider(e:MouseEvent):void
		{
			
		}
		private function onBackDown(e:MouseEvent):void
		{
			
		}
		override public function draw():void
		{
			if(isInvalid( BACKABLED ))
			{
				setBackable();
			}
		}
		private function setBackable():void
		{
			if(mBackabled)
			{
				if(mBackSkin is Sprite)
				{
					mBackSkin.addEventListener(MouseEvent.CLICK, onBackDown);
				}else
				{
					trace("backskin不是Sprite,不支持鼠标事件");
				}
			}else
			{
				if(mBackSkin is Sprite)
				{
					mBackSkin.mouseEnabled = false;
				}
			}
		}		
	}
}